package com.xinghen.common.zookeeper.registry;

import com.xinghen.common.base.properties.XinghenCloudProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.net.InetAddress;

/**
 * @author xinghen
 * @ClassName: ZookeeperInitRunner
 * @Package: com.xinghen.common.zookeeper.registry
 * @Description zookeeper 启动器
 * @create 2018-04-12 17:12
 */
@Component
@Order
@Slf4j
public class ZookeeperInitRunner implements CommandLineRunner {

    @Autowired
    private XinghenCloudProperties xinghenCloudProperties;

    @Value("${spring.application.name}")
    private String applicationName;

    @Override
    public void run(String... args) throws Exception {
        String hostAddress = InetAddress.getLocalHost().getHostAddress();
        log.info(">>>>>>>>>> ZookeeperInitRunner，init. HostAddress={}, applicationName={} <<<<<<<<<<",
                hostAddress, applicationName);
        try {
            RegistryCenterFactory.startUp(xinghenCloudProperties, hostAddress, applicationName);
            log.info(">>>>>>>>>> ZookeeperInitRunner，finish <<<<<<<<<<");
        } catch (Exception e) {
            log.error(">>>>>>>>>> ZookeeperInitRunner，failed <<<<<<<<<<");
        }
    }
}
